<template>
  <div class="flv-wrap">
    <video :id="uuid" name="videoElement" controls autoplay>
      Your browser is too old which doesn't support HTML5 video.
    </video>
  </div>
</template>

<script>

  import flvjs from "flv.js";
  import {uuid} from "../../utils";

  export default {
    name: "FlvPlayer",
    player: null,
    data() {
      return {
        uuid: uuid()
      }
    },
    created() {
      this.initFlv();

    },
    props: {
      flvUrl: {
        type: String,
        default: ''
      }
    },
    mounted() {
      if (this.flvUrl) {
        this.flvLoad(this.flvUrl);
        this.flvStart();
      }
    },
    methods: {
      initFlv() {
        flvjs.LoggingControl.addLogListener(function (type, str) {
          console.log(str);
        });
      },

      flvLoad(url) {
        let mediaDataSource = {
          type: 'flv',
          url
        };
      },
      //
      flvLoadMds(mediaDataSource) {
        let player = this.$options.player;
        if (player !== null) {
          player.unload();
          player.detachMediaElement();
          player.destroy();
          player = null;
        }

        player = flvjs.createPlayer(mediaDataSource, {
          enableWorker: false,
          lazyLoadMaxDuration: 3 * 60,
          seekType: 'range',
        });
        const element = document.getElementById(this.uuid);
        player.attachMediaElement(element);
        player.load();
      },

      flvStart() {
        let player = this.$options.player;
        player && player.play();
      },

      flvPause() {
        let player = this.$options.player;
        player && player.pause();
      },

      flvDestroy() {
        let player = this.$options.player;
        if (player) {
          player.pause();
          player.unload();
          player.detachMediaElement();
          player.destroy();
          player = null;
        }
      }
    },

    destroyed() {
      this.flvDestroy();
    }
  }
</script>

<style lang="scss" scoped>

  .flv-wrap {
    width: 100%;
    height: 100%;

    video {
      width: 100%;
      height: 100%;
    }
  }
</style>
